#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"


void LocalScene::OnInit()
{

	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");
	mRayTracingProcess = GetProcess<RayTracingProcess>("RayTracingProcess");


	mCamera.SetPosition(glm::vec3(0.f, 0.04f, 7.f));
	mCamera.LookAt(glm::vec3(0.f, 0.04f, 7.f), glm::vec3(0.f, 0.04f, 0.f));
	mCamera.UpdateViewMatrix();

	GeometryGenerator geometryGenerator;


	GeometryGenerator::MeshData lightData = geometryGenerator.CreatePanel(1.f, 1.f, glm::vec3(0.f, -1.f, 0.f));

	LocalEntity* lightEntity = CreateEntity<LocalEntity>("light");
	lightEntity->AddComponent<Model>(lightData);
	lightEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 2.f, -4.f));
	lightEntity->GetTransform().UpdateMatrices();
	lightEntity->SetColor(glm::vec4(1.f));

	lightEntity->mMaterial.emissive = glm::vec3(10.f);
	lightEntity->mMaterial.baseColor = glm::vec3(1.f);


	GeometryGenerator::MeshData bottomPanelData = geometryGenerator.CreatePanel(10.f, 10.f, glm::vec3(0.f, 1.f, 0.f));

	LocalEntity* bottomPanelEntity = CreateEntity<LocalEntity>("bottomPanel");
	bottomPanelEntity->AddComponent<Model>(bottomPanelData);
	bottomPanelEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -2.f, 0.f));
	bottomPanelEntity->GetTransform().UpdateMatrices();
	bottomPanelEntity->SetColor(glm::vec4(0.90f, 0.72f, 0.88f, 1.f));

	bottomPanelEntity->mMaterial.baseColor = glm::vec3(0.6f);
	bottomPanelEntity->mMaterial.metallic = 0.5f;
	bottomPanelEntity->mMaterial.roughness = 0.25f;
	bottomPanelEntity->mMaterial.clearcoatGloss = 1.f;
	// bottomPanelEntity->mMaterial.roughness = 1.f;


	GeometryGenerator::MeshData sphereData = geometryGenerator.CreateSphere(0.8f, 20, 20);

	LocalEntity* sphereEntity = CreateEntity<LocalEntity>("sphere");
	// sphereEntity->AddComponent<Model>(sphereData);
	sphereEntity->AddComponent<Model>("Teapot.obj");
	sphereEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, -1.9f, 0.f));
	sphereEntity->GetTransform().SetLocalScale(glm::vec3(0.3f));
	sphereEntity->GetTransform().UpdateMatrices();
	sphereEntity->SetColor(glm::vec4(1.f, 0.f, 0.f, 1.f));

	sphereEntity->mMaterial.baseColor = glm::vec3(1.0f, 0.765557f, 0.336057f);
	sphereEntity->mMaterial.metallic = 0.7f;
	sphereEntity->mMaterial.roughness = 0.3f;

}

void LocalScene::OnExit()
{

}

void LocalScene::OnTick(float deltaTime)
{

}

void LocalScene::OnImgui()
{
	// GameScene::OnImgui();
	// ImGui::ShowDemoWindow();

	if (ImGui::BeginMainMenuBar())
	{
		ImGui::Text("Fps : %d ", GRenderState->GetFps());

		ImGui::Text("Spp : %d ", mRayTracingProcess->mFrame);

		ImGui::SetNextItemWidth(100);
		if (ImGui::InputInt("Max Frame", &mRayTracingProcess->mMaxFrame, 100, 100)) { mRayTracingProcess->mFrame = 0; };

		ImGui::Text("Random : ");

		if (ImGui::RadioButton("Sobol R", &mRayTracingProcess->mDebugMode, 0)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Sobol L", &mRayTracingProcess->mDebugMode, 1)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Sobol", &mRayTracingProcess->mDebugMode, 2)) { mRayTracingProcess->mFrame = 0; }
		if (ImGui::RadioButton("Lcg", &mRayTracingProcess->mDebugMode, 3)) { mRayTracingProcess->mFrame = 0; }

		ImGui::EndMainMenuBar();
	}

#if 0

	ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);

	ImGui::Begin("MapOption");



	ImGui::End();

#endif
	}
